package io.itit.ecp.admin.provide.action.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.CarbonSinkQueryReq;
import io.itit.ecp.admin.provide.action.resp.CarbonSinkResp;
import io.itit.ecp.admin.provide.helper.SceneHelper;
import io.itit.ecp.admin.server.entity.CarbonSinkEntity;
import io.itit.ecp.admin.server.entity.SceneProjectEntity;
import io.itit.ecp.admin.server.service.ICarbonSinkService;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 碳汇管理
 */
@Api(tags = "碳汇管理")
@RestController
@RequestMapping("/ecp-admin/carbonSink")
public class CarbonSinkController {

    @Resource
    private ICarbonSinkService carbonSinkService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:carbonSink:search")
    public ResultResp<IPage<CarbonSinkResp>> queryPage(@RequestBody @Validated(IQueryPage.class) CarbonSinkQueryReq req) {
        Page<CarbonSinkResp> page = buildQW(req).joinPage(PageUtils.getPage(req, CarbonSinkResp.class), CarbonSinkResp.class);
        return ResultResp.success(SceneHelper.i().setSceneInfo(page));
    }

    @ApiOperation("分页查询")
    @PostMapping("queryList")
    public ResultResp<List<CarbonSinkResp>> queryList(@RequestBody CarbonSinkQueryReq req) {
        Set<String> sceneEdificeIds = Arrays.stream(StringUtils.tokenizeToStringArray(req.getSceneEdificeId(), ",")).collect(Collectors.toSet());
        Set<String> sceneFloorIds = Arrays.stream(StringUtils.tokenizeToStringArray(req.getSceneFloorId(), ",")).collect(Collectors.toSet());
        Set<String> sceneRoomIds = Arrays.stream(StringUtils.tokenizeToStringArray(req.getSceneRoomId(), ",")).collect(Collectors.toSet());
        List<CarbonSinkResp> page = ConvertUtils.convert(carbonSinkService.lambdaQuery()
                .eq(StringUtils.hasLength(req.getSceneProjectId()), CarbonSinkEntity::getSceneProjectId, req.getSceneProjectId())
                .in(!sceneRoomIds.isEmpty(), CarbonSinkEntity::getSceneEdificeId, sceneEdificeIds)
                .in(!sceneFloorIds.isEmpty(), CarbonSinkEntity::getSceneFloorId, sceneFloorIds)
                .in(!sceneRoomIds.isEmpty(), CarbonSinkEntity::getSceneRoomId, sceneRoomIds)
                .list(), CarbonSinkResp.class);
        return ResultResp.success(SceneHelper.i().setSceneInfo(page));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:carbonSink:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) CarbonSinkEntity req) {
        return ResultResp.success(carbonSinkService.save(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:carbonSink:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) CarbonSinkEntity req) {
        return ResultResp.success(carbonSinkService.updateById(req));
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:carbonSink:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(carbonSinkService.removeById(req.getId()));
    }

    private static JoinLambdaWrapper<CarbonSinkEntity> buildQW(CarbonSinkQueryReq req) {
        return Joins.of(CarbonSinkEntity.class)
                .leftJoin(SceneProjectEntity.class, SceneProjectEntity::getId, CarbonSinkEntity::getSceneProjectId)
                .selectAs(SceneProjectEntity::getProjectName, CarbonSinkResp::getProjectName)
                .end()
                .eq(StringUtils.hasLength(req.getSceneProjectId()), CarbonSinkEntity::getSceneProjectId, req.getSceneProjectId())
                .eq(StringUtils.hasLength(req.getSceneEdificeId()), CarbonSinkEntity::getSceneEdificeId, req.getSceneEdificeId())
                .eq(StringUtils.hasLength(req.getSceneFloorId()), CarbonSinkEntity::getSceneFloorId, req.getSceneFloorId())
                .eq(StringUtils.hasLength(req.getSceneRoomId()), CarbonSinkEntity::getSceneRoomId, req.getSceneRoomId())
                .like(StringUtils.hasLength(req.getType()), CarbonSinkEntity::getType, req.getType())
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), CarbonSinkEntity::getCreateTm);
    }
}
